//
//  WJNetworkContext.m
//  117goFramework
//
//  Created by Yunhai.Wu on 15/7/29.
//  Copyright (c) 2015年 117go. All rights reserved.
//

#import "QWNetworkContext.h"
#import "QWNetworkContextConfig.h"

@implementation QWNetworkContext

DEF_SINGLETON_INIT(QWNetworkContext)

-(BOOL)isConnection {
    return [self currentStatus] != NotReachable;
}

-(NetworkStatus) currentStatus {
    return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];
}

-(void) addNotification:(id) target selector:(SEL) selector {
    if (target != nil && selector != NULL) {
        [[NSNotificationCenter defaultCenter] addObserver:target selector:selector name:kReachabilityChangedNotification object:nil];
    }
}
-(void) removeNotification:(id)target {
    if (target) {
        [[NSNotificationCenter defaultCenter] removeObserver:target name:kReachabilityChangedNotification object:nil];
    }
}

-(void)singleInit {
    NSString *host = [[QWNetworkContextConfig sharedInstance] serverHost];
    if (host) {
        Reachability *reachability = [Reachability reachabilityWithHostname:host];
        [reachability startNotifier];
    } else {
        Reachability *reachability = [Reachability reachabilityForInternetConnection];
        [reachability startNotifier];
    }
}


@end
